package com.goldplusgold.network.lib.http.netty;



import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2017/7/13.
 */

public final class ChannelBootstrapManager {

    private static ChannelBootstrapManager channelBootstrapManager;
    private HashMap<String,ChannelBootstrapInterface> channels = new HashMap<>();
    private ChannelBootstrapManager(){

    }

    public static ChannelBootstrapManager getInstance(){
        if(channelBootstrapManager == null){
            synchronized (ChannelBootstrapManager.class){
                if(channelBootstrapManager == null){
                    channelBootstrapManager = new ChannelBootstrapManager();
                }
            }
        }
        return channelBootstrapManager;
    }

    public void register(String className,final ChannelBootstrapInterface listener){
        if(listener != null){
            if(!channels.containsKey(className)){
                Executors.newSingleThreadExecutor().submit(new Runnable() {
                    @Override
                    public void run() {
                        listener.start();
                    }
                });
                channels.put(className,listener);
            }
        }

    }

    public void unregister(String className){
        if(channels.containsKey(className)){
            ChannelBootstrapInterface listener = channels.remove(className);
            if(listener != null){
                listener.close();
            }
        }

    }


}
